#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void error(int number){
    switch(number){
        case 1:
             printf("命令行参数个数错误\n");
             break;
        case 2:
            printf("第二个参数错误\n");
            break;
        case 3: 
            printf("输入文件路径错误\n");
            break;
        case 4:
            printf("输出问价路径错误\n");
            break;
    }
}

int main(int argc, char *argv[]){
    switch(argc){
        case 2:
            if(strcmp(argv[1], "-d")!= 0 && strcmp(argv[1], "-r") != 0){
                error(2);
            }else{
                int i, n;
                char **src;
                printf("请输入你要输入的文本的行数，n = ");
                scanf("%d", &n);
                getchar();
                src = (char **)malloc(n * sizeof(char *));
                for (i = 0; i < n; i++){
                    printf("__:");
                    src[i] = (char *)malloc(30 * sizeof(char));
                    gets(src[i]);
                }
                for (i = 0; i < n; i++){
                    if(strcmp(argv[1], "-r") == 0){
                        char dst[20];
                        to_roman_numeral(dst, i + 1);
                        printf("%s\t", dst);
                    }else{
                        printf("%d\t", i + 1);
                    }
                    puts(src[i]);
                }
            }
            break;
        case 3:
            if(strcmp(argv[1], "-d")!= 0 && strcmp(argv[1], "-r") != 0){
                error(2);
            }else{
                FILE *fp1 = fopen(argv[2], "r");
                if(fp1 == NULL){
                    error(3);
                }else{
                    int i = 0;
                    char text[500];
                    fgets(text, 500, fp1);//将'\n'读入并保存在了text中，另外末尾自动加上'\0'
                    while(!feof(fp1)){
                        if(strcmp(argv[1], "-r") == 0){
                            char dst[20];
                            to_roman_numeral(dst, ++i);
                            printf("%s\t", dst);
                        }else{
                            printf("%d\t", ++i);
                        }
                        printf("%s", text);
                        fgets(text, 500, fp1);//将'\n'读入并保存在了text中，另外末尾自动加上'\0'
                    }   
                }
            }
            break;
        case 4:
            if(strcmp(argv[1], "-d")!= 0 && strcmp(argv[1], "-r") != 0){
                error(2);
            }else{
                FILE *fp1 = fopen(argv[2], "r");
                FILE *fp2 = fopen(argv[3], "w");
                if(fp1 == NULL){
                    error(3);
                }else if(fp2 == NULL){
                    error(4);
                }else{
                        int i = 0;
                    char text[500];
                    fgets(text, 500, fp1);//将'\n'读入并保存在了text中，另外末尾自动加上'\0'
                    while(!feof(fp1)){
                        if(strcmp(argv[1], "-r") == 0){
                            char dst[20];
                            to_roman_numeral(dst, ++i);
                            fprintf(fp2 ,"%s\t", dst);
                        }else{
                            fprintf(fp2, "%d\t", ++i);
                        }
                        fprintf(fp2, "%s", text);
                        fgets(text, 500, fp1);//将'\n'读入并保存在了text中，另外末尾自动加上'\0'
                    }
                }
            }
            break;
        default:
            error(1);
            break;
    }
    return 0;
}
